//导入

var homehref = '';
$(function(){
    var isLogin = urls.checklogin();
    if(!isLogin) {
        $('.alert-title').html('未登录，请先登录');
        var $dialog = $('.modal-alert');
        $dialog.show();
        $dialog.find('.weui_btn_dialog').unbind('click').bind('click', function () {
            location.href = '../login';          
        });
        setTimeout(function(){
            location.href = '../login';
        }, 3000);
        return;
    }
    Page.token = urls.GetQueryString('t');
    $('a.to-homepage').attr('href', '../home?t=' + Page.token);
    $('.add-merchants').attr('href', 'modify?t=' + Page.token);



    Page.initCss();
    Page.id = urls.GetQueryString('key');
    // Page.parent_id = urls.GetQueryString('parentId');
    // Page.parent_name = urls.GetQueryString('parentName');
    // Page.name = urls.GetQueryString('name');

    // Page.loadImg();
    Page.loadData();


    /*var prm = location.href.split('#');
    if(prm.length > 1) {
        for(var i=1, l_p=prm.length; i<l_p; i++) {
            var str = prm[i];
            homehref += '#' + str;
        }
    }
    $('.homepage').attr('href', '../' + homehref);*/


    /*$('.moto-name').html(Page.name);
    $('.parent-name').attr('href', '../model/?key=' + Page.parent_id + '&'+ homehref).html(Page.parent_name);
    $('.my-name').html('&gt;'+ Page.name);*/


    // var url_param = '?key=' + Page.id + '&parentId=' + Page.parent_id + '&parentName=' + Page.parent_name + '&name=' + Page.name +'&'+ homehref;
    // var url_param = '?key=' + Page.id;
    // $('.tab-zongshu').attr('href', '../zongshu/' + url_param);
    // $('.tab-canshu').attr('href', '../canshu/'  + url_param);
});

var Page = {
    id: false,
    name: false,
    parent_id: false,
    parent_name: false
};

Page.initCss = function() {
    var bodyWidth = Page.getWindowWidth();
    var bodyHeight = Page.getWindowHeight();
    $('.w-rest-160').width(bodyWidth - 180);
    // $('.param').css({'min-height': (bodyHeight - 360)+'px'})
}

Page.loadData = function() {
    var url = urls.server + '/merchants';
    $.ajax({
        type: 'GET',
        async: true,
        url: url,
        dataType: 'json',
        success: function(e){
            if(e && e.length > 0) {
                Page.showData(e);
            }
            else {
                $('.data-list').html('<div class="error">未查询到数据</div>');
            }
        }
    });
}

Page.showData = function(datas) {
    var h = '';
    for( var i=0, len=datas.length; i<len; i++ ) {
        var item = datas[i];

        h += '<div class="line-wrapper">';
        h += '  <div class="line-scroll-wrapper">';
        h += '      <div class="line-normal-wrapper">';
        h += '          <div class="title">'+ item.name +'</div>';
        h += '          <div class="param">';
        h += '              <span class="lbl">编号：</span>';
        h += '              <span class="text">'+ item.number +'</span>';
        h += '              <span class="lbl">电话：</span>';
        h += '              <span class="text">'+ item.phone +'</span>';
        h += '          </div>';
        h += '      </div>';
        h += '      <div class="line-function">';
        h += '          <a class="btn btn-edit" href="modify?key='+ item.id +'&t='+ Page.token +'">编辑</a>';
        h += '      </div>';
        h += '  </div>';
        h += '</div>';
    }

    h += '<div class="error">没有更多数据了</div>'

    $('.data-list').html(h);

    setLiLeftSliding();
}

Page.add = function() {
    var url = urls.server + '/merchants';
    $.ajax({
        type: 'POST',
        async: true,
        url: url,
        dataType: 'json',
        data: {
            access_token: 'AABBCCDD'
        },
        success: function(e){
            console.log(e);
        }
    });
}








//获取页面显示区域高度
Page.getWindowHeight = function() {
    if (navigator.appName == "Microsoft Internet Explorer" ) {
        return document.compatMode == "CSS1Compat" ? document.documentElement.clientHeight : document.body.clientHeight;
    }
    else {
        return self.innerHeight;
    } 
}

//获取页面显示区域宽度
Page.getWindowWidth = function() {
    if (navigator.appName == "Microsoft Internet Explorer") {
        return document.compatMode == "CSS1Compat" ? document.documentElement.clientWidth : document.body.clientWidth;
    }
    else {
        return self.innerWidth;
    } 
}

var setLiLeftSliding = function(){
    // 设定每一行的宽度=屏幕宽度+按钮宽度
    $(".line-scroll-wrapper").width($(".line-wrapper").width() + $(".line-function").width());
    // 设定常规信息区域宽度=屏幕宽度
    $(".line-normal-wrapper").width($(".line-wrapper").width());


    // 获取所有行，对每一行设置监听
    var lines = $(".line-normal-wrapper");
    var len = lines.length; 
    var lastX, lastXForMobile;

    // 用于记录被按下的对象
    var pressedObj;  // 当前左滑的对象
    var lastLeftObj; // 上一个左滑的对象

    // 用于记录按下的点
    var start;

    var moveWidthLeft = -50;
    var moveWidthRight = 60;

    // 网页在移动端运行时的监听
    for (var i = 0; i < len; ++i) {
        lines[i].addEventListener('touchstart', function(e){
            lastXForMobile = e.changedTouches[0].pageX;
            pressedObj = this; // 记录被按下的对象 

            // 记录开始按下时的点
            var touches = event.touches[0];
            start = { 
                x: touches.pageX, // 横坐标
                y: touches.pageY  // 纵坐标
            };
        });

        lines[i].addEventListener('touchmove',function(e){
            // 计算划动过程中x和y的变化量
            var touches = event.touches[0];
            delta = {
                x: touches.pageX - start.x,
                y: touches.pageY - start.y
            };

            // 横向位移大于纵向位移，阻止纵向滚动
            if (Math.abs(delta.x) > Math.abs(delta.y)) {
                event.preventDefault();
            }
        });

        lines[i].addEventListener('touchend', function(e){
            var diffX = e.changedTouches[0].pageX - lastXForMobile;
            if (diffX < moveWidthLeft) {
                $(pressedObj).animate({marginLeft:"-80px"}, 500); // 左滑
                lastLeftObj && lastLeftObj != pressedObj && 
                    $(lastLeftObj).animate({marginLeft:"0"}, 500); // 已经左滑状态的按钮右滑
                lastLeftObj = pressedObj; // 记录上一个左滑的对象
            } else if (diffX > moveWidthRight) {
                $(pressedObj).animate({marginLeft:"0"}, 500); // 右滑
                lastLeftObj = null; // 清空上一个左滑的对象
            }
        });
    }


    // 网页在PC浏览器中运行时的监听
    for (var i = 0; i < len; ++i) {
        $(lines[i]).bind('mousedown', function(e){
            lastX = e.clientX;
            pressedObj = this; // 记录被按下的对象
        });

        $(lines[i]).bind('mouseup', function(e){
            var diffX = e.clientX - lastX;
            if (diffX < moveWidthLeft) {
                $(pressedObj).animate({marginLeft:"-80px"}, 500); // 左滑
                lastLeftObj && lastLeftObj != pressedObj && 
                    $(lastLeftObj).animate({marginLeft:"0"}, 500); // 已经左滑状态的按钮右滑
                lastLeftObj = pressedObj; // 记录上一个左滑的对象
            } else if (diffX > moveWidthRight) {
                $(pressedObj).animate({marginLeft:"0"}, 500); // 右滑
                lastLeftObj = null; // 清空上一个左滑的对象
            }
        });
    }

}